layui.use(['element', 'layer', 'form', 'layedit', 'jquery'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var layedit = layui.layedit;
    var element = layui.element;
    $(document).ready(function () {
        if (window.location.search.indexOf("?") < 0) { //判断是否是第一次加载
            var index = layer.load(1);
            setTimeout(function () {
                $('#middleConter').attr('src', 'middlegroundIndex/middleConter.html?ranparam=random()');
                layer.close(index);
            }, 500);
        } else { }
    });
    dynamicMenu();
    $('.demoA').hover(function () {
        var thisID = $(this).attr("id");
        var str = demoObj(thisID);
        $(this).siblings().html(str);
    });
});

// function demoObj(id) {
// 	var id = 7
//     var object = store.get("userInfo", true); // 用户信息
// 	var URL = '';
// 	if (id == 1) {
// 		URL = 'middlegroundIndex/standardAM.json';
// 		ICON = 'layui-icon-read';
// 	} else if (id == 2) {
// 		URL = 'middlegroundIndex/middlegroundIndex.json';
// 		ICON = 'layui-icon-log';
// 	} else if (id == 3) {
// 		URL = 'middlegroundIndex/controlCenter.json';
// 		ICON = 'layui-icon-component';
// 	} else if (id == 4) {
// 		URL = 'middlegroundIndex/qualityCenter.json';
// 		ICON = 'layui-icon-set-sm';
// 	} else if (id == 5) {
// 		URL = 'middlegroundIndex/calculateCenter.json';
// 		ICON = 'layui-icon-list';
// 	} else if (id == 6) {
// 		URL = 'middlegroundIndex/monitoringCenter.json';
// 		ICON = 'layui-icon-chart';
// 	} else if (id == 7) {
// 		URL = 'middlegroundIndex/dataManagement.json';
// 		ICON = 'layui-icon-notice';
// 	}
// 	var strData = '';
// 	$.ajax({
// 		url: URL,
// 		dataType: 'json',
// 		type: 'get',
// 		async: false,
// 		success: function(data) {
// 			var divList = 'divList';
// 			var num = data.length;
// 			var str = '';
// 			str += '<div class="divList">';
// 			for (var k = 0; k < num; k++) {
// 				str += '<div id=' + divList + '>';
// 				if (data[k].href) {
// 					for (var j = 0; j < data[k].divList.length; j++) {
// 						if (j == 0) {
// 							str += '<a class="divListA" style="color:#2667DF !important" href="javascript:;">';
// 							str += '<i class="divListAImg layui-icon ' + ICON + '"></i>';
// 							str += data[k].divList[j] + '</a>';
// 						} else {
// 							str += '<a onclick="skip(this)" title=' + data[k].divList[j] + ' href="javascript:;" name=' + data[k].href[
// 								j] + '>' + data[k].divList[j] + '</a>';
// 						}
// 					}
// 				} else {
// 					for (var j = 0; j < data[k].divList.length; j++) {
// 						if (j == 0) {
// 							str += '<a class="divListA" style="color:#2667DF !important" href="javascript:;">';
// 							str += '<i class="divListAImg layui-icon ' + ICON + '"></i>';
// 							str += data[k].divList[j] + '</a>';
// 						} else {
// 							str += '<a title=' + data[k].divList[j] + ' href="javascript:;">' + data[k].divList[j] + '</a>';
// 						}
// 					}
// 				}
//
// 				str += '</div>';
// 			};
// 			str += '</div>';
// 			strData = str;
// 		}
// 	});
// 	return strData;
// }
function demoObj(id) {
    //ICON = 'layui-icon-notice'
    var object = store.get("userInfo", true); // 用户信息
    var strData = '';
    $.ajax({
        url: 'http://' + window.location.host + '/uum/menu/getMenuByParentCode/' + id + '/' + object.userId + '/' + 1013,
        dataType: 'json',
        type: 'get',
        async: false,
        success: function (data) {
            var data = toJsonTree(data);
            var divList = 'divList';
            var num = data.length;
            var str = '';
            if (num > 0) {
                str += '<div class="divList">';
                for (var k = 0; k < num; k++) {
                    str += '<div id=' + divList + '>';
                    str += '<a class="divListA" style="color:#2667DF !important" href="javascript:;">';
                    if (data[k].tags.icon) {
                        str += '<i class="divListAImg"><img src="data:image/png;base64,'
                        str += data[k].tags.icon
                        str += '"></i>';
                    }
                    str += data[k].tags.name + '</a>';
                    if (data[k].children.length > 0) {
                        for (var j = 0; j < data[k].children.length; j++) {
                            str += '<a onclick="skip(this)" title=' + data[k].children[j].tags.name + ' href="javascript:;" name=' + data[k].children[j].tags.menu_url + '&code=' + data[k].children[j].tags.code + '>' + data[k].children[j].tags.name + '</a>';
                        }
                    }
                    str += '</div>';
                };
                str += '</div>';
            }
            strData = str;
        }
    });
    return strData;
}

// 获取token
var yaoshis = ''
var parmss = {
    userName:'admin',
    userPassword:'dolphinscheduler123'
}

$.ajax({
    type: "post",
    url: 'http://124.254.6.162:12345/dolphinscheduler/login',
    // dataType: 'x-www-form-urlencoded',
    cache: false,
    // contentType: "application/json",
    data: parmss,
    success: function (redata) {
        yaoshis = redata.data.sessionId
    }
});
// 三级导航跳转
function skip($this) {
    


console.log(yaoshis,'yaoshisyaoshisyaoshis');
    var name = $($this).attr("name");
    var newSrc = name+'&yaoshi='+yaoshis
    console.log(newSrc,'newSrcnewSrcnewSrcnewSrc');
    sessionStorage.setItem("sessionId", 'd4af0a80bdafd2b34890449294f09daa')
    var href = $($this).parent().parent().parent().parent().find('.demoA').attr('name');
    if (href == 'http://10.108.6.131:7180/cmf/') {
        window.open(href + name, '_blank');
    } else if (name.indexOf('type=1') != -1) {
        // window.open(name + '&yaoshi=' + yaoshis, '_blank');
        window.open(newSrc, '_blank');
    } else {
        window.open(href + '?href=' + name, '_blank');
    }
}

function GetRequest(urlStr) {
    if (typeof urlStr == "undefined") {
        var url = decodeURI(location.search); //获取url中"?"符后的字符串
    } else {
        var url = "?" + urlStr.split("?")[1];
    }
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
// 退出系统
function layuiBack() {
    layer.confirm('确定退出吗?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        window.location.href = "/dataCenter/login.html";
        layer.msg('退出成功！');
        // ws.util.startOut();
        //layer.msg('退出成功！');
    });
};
//动态显示菜单
function dynamicMenu() {
    var object = store.get("userInfo", true); // 用户信息
    $.ajax({
        url: 'http://' + window.location.host + '/uum/menu/getAppMenusByuserId?appId=1013&userId=' + object.userId + '&_=' + new Date().getTime(),
        dataType: 'json',
        type: 'get',
        async: false,
        success: function (data) {
            if (data.code == '0000') { //如果接口返回成功
                var data = data.data;
                data = toJsonTree(data)
                console.log(data, 'datadatadata');

                var dynamicMenu = $('#dynamicMenu');
                var str = '';
                for (var i = 0; i < data.length; i++) {
                    if (!data[i].parent_id) {
                        str += '<li class="layui-nav-item" lay-unselect>';
                        str += '<a class="demoA" id="';
                        str += data[i].id;
                        str += '"name="' + data[i].tags.menu_url;
                        str += '" href="';
                        if (data[i].children.length == 0) {
                            if (data[i].tags.menu_url) {
                                str += data[i].tags.menu_url + '"target="_blank">'
                            } else {
                                str += 'javascript:;">'
                            }
                        } else {
                            str += 'javascript:;">'
                        }
                        if (data[i].tags.icon) {
                            str += '<i class="loginImg">';
                            str += '<img src="data:image/png;base64,'
                            str += data[i].tags.icon
                            str += '">'
                            str += '</i>';
                        }
                        str += data[i].tags.name
                        if (data[i].children.length > 0) {
                            str += '<i class="layui-icon layui-icon-triangle-d"></i>'
                        }
                        str += '</a>'
                        if (data[i].children.length > 0) {
                            str += '<div class="layui-nav-child layui-anim layui-anim-up"></div>'
                        }
                        str += '</li>'
                    }
                }
                dynamicMenu.html(str);
            }
        }
    });
    $('#carousel ul li').mouseover(function () {
        $('.layui-nav-child').hide();
        $(this).find('.layui-nav-child').show();
    });
    $('#carousel ul li .layui-nav-child').hover(function () {
        $(this).show();
    }, function () {
        $(this).hide();
    });
}
function toJsonTree(regionData) {
    var result = [];
    if (regionData && regionData.length > 0) {
        var rowMap = {};
        for (var i = 0; i < regionData.length; i++) {
            var row = regionData[i];
            var id = row.id;
            rowMap[id] = {
                id: row.id,
                text: row.name,
                tags: row,
                children: []
            };
        };
        for (var i = 0; i < regionData.length; i++) {
            var row = regionData[i];
            var id = row.id;
            var pid = row.parent_id;
            var prow = rowMap[pid];
            if (!pid || !prow) {
                result.push(rowMap[id]);
            }
            if (prow) {
                prow.children = prow.children || [];
                prow.children.push(rowMap[id]);
            }
        };
    }
    return result;
};
